package org.oneboot.core.event;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

@Component
public class SpringContextUtils implements BeanFactoryPostProcessor {

	private static ConfigurableListableBeanFactory beanFactory;

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory)
			throws BeansException {
		SpringContextUtils.beanFactory = configurableListableBeanFactory;
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) throws BeansException {
		return (T) beanFactory.getBean(name);
	}

	public static <T> T getBean(Class<T> clz) throws BeansException {
		T result = beanFactory.getBean(clz);
		return result;
	}

	public static <T> List<T> getBeansOfType(Class<T> type) {
		return beanFactory.getBeansOfType(type).entrySet().stream().map(entry -> entry.getValue())
				.collect(Collectors.toList());
	}

	// 上面的例子用到了这个
	public static List<Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
		Map<String, Object> beansWithAnnotation = beanFactory.getBeansWithAnnotation(annotationType);

		// java 8 的写法，将 map 的 value 收集起来到一个 list 中
		return beansWithAnnotation.entrySet().stream().map(entry -> entry.getValue()).collect(Collectors.toList());

	}
}
